package com.yaoxun.bored.controller.sys;

import java.util.Date;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.yaoxun.bored.common.BaseController;
import com.yaoxun.bored.common.JsonObject;
import com.yaoxun.bored.common.PageResult;
import com.yaoxun.bored.model.sys.dict.Dict;
import com.yaoxun.bored.model.sys.dict.DictQuery;
import com.yaoxun.bored.service.sys.dict.DictService;
import com.yaoxun.bored.util.enums.JsonStatus;
import com.yaoxun.bored.validator.ValidatorHelper;

import tk.mybatis.mapper.entity.Example;

/**
 * 字典controller
 * @author Loren
 *
 */
@Controller
@RequestMapping("/dict")
public class DictController extends BaseController {
	
	private static String EDIT_PAGE = "sys/dict/dict_edit";
	
	private static String INDEX_PAGE = "sys/dict/dict";
	
	@Inject
	private ValidatorHelper validatorHelper;
	
	@Inject
	private DictService dictService;
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	@RequestMapping(value="/save", method=RequestMethod.GET)
	public ModelAndView save() {
		ModelAndView mv = new ModelAndView(EDIT_PAGE);
		return mv;
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject save(Dict dict) {
		JsonObject jsonObject = new JsonObject();
		BindingResult bindingResult = validatorHelper.validate(dict);
		if(bindingResult.hasErrors()) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, bindingResult.getFieldError().getDefaultMessage());
			return jsonObject;
		}
		dict.setCreateTime(new Date());
		dict.setCreateId(getLoginUserId());
		try {
			dictService.insert(dict);
		} catch(DuplicateKeyException e) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, "字典码已经存在");
			return jsonObject;
		}
		return jsonObject;
	}
	
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam Integer id) {
		ModelAndView mv = new ModelAndView(EDIT_PAGE);
		Dict dict = dictService.selectByPrimaryKey(id);
		mv.addObject("obj", dict);
		return mv;
	}
	
	
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject edit(Dict dict) {
		JsonObject jsonObject = new JsonObject();
		BindingResult bindingResult = validatorHelper.validate(dict);
		if(bindingResult.hasErrors()) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, bindingResult.getFieldError().getDefaultMessage());
			return jsonObject;
		}
		try {
			dictService.updateByPrimaryKeySelective(dict);
		} catch(DuplicateKeyException e) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, "字典码已经存在");
			return jsonObject;
		}
		return jsonObject;
	}
	
	/**
	 * 删除字典
	 * @param dictIds
	 * @return
	 */
	@RequestMapping(value="del", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject delete(@RequestParam(value="dictIds[]") Integer[] dictIds) {
		JsonObject jsonObject = new JsonObject();
		dictService.delete(dictIds);
		return jsonObject;
	}
	
	/**
	 * 判断字典码是否已经存在
	 * @param dictCode
	 * @return
	 */
	@RequestMapping(value="/exist", method=RequestMethod.GET)
	@ResponseBody
	public String exist(@RequestParam String dictCode) {
		Example example = new Example(Dict.class);
		example.createCriteria().andEqualTo("dictCode", dictCode);
		int count = dictService.selectCountByExample(example);
		if(count > 0) {
			return JsonStatus.FAIL.value();
		}
		return JsonStatus.SUCCESS.value();
	}
	
	@RequestMapping("/search")
	@ResponseBody
	public PageResult search(DictQuery query) {
		PageResult result = new PageResult();
		query.setOrderBy("dict_id desc");
		PageInfo<Map<String, Object>> info = dictService.selectByPaging(query);
		result.setTotal(info.getTotal());
		result.setRows(info.getList());
		return result;
	}
	
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView(INDEX_PAGE);
		return mv;
	}
	
}
